var util = require('../../../utils/util.js');  
var mathUtil = require('../../../utils/math.js');
var pageObject = {
  data: {
    defaultSize: 'default',
    primarySize: 'default',
    warnSize: 'default',
    disabled: false,
    plain: false,
    loading: false,
    cardId:'',
    searchLogList:[],
    showLogList: [],
    showSearchLog:false,
  },

  //在加载后或者toolbar切换后触发
  onShow: function() {
    var t = this;
    //上次输入的卡id
    wx.getStorage({
      key: 'cardNum',
      success: function (res) {
        console.log("获取缓存的cardNum:" + res.data)
        if(res.data != null && res.data != '') {
          t.setData({
            cardId: res.data
          });
        }
      }
    });

    //缓存的卡num列表
    wx.getStorage({
      key: 
    'logCache',
      success: function (res) {
        if (res.data != null && res.data.length > 0) {
          t.setData({
            searchLogList: res.data,
            showLogList: res.data
          });
        }
      },
    });
  },

  //输入卡号时触发
  cardIdInput : function(e) {
    var result = mathUtil.wordsContains(this.data.searchLogList, e.detail.value);
    console.log("除阿发" + result)
    this.setData({
      cardId:e.detail.value,
      showLogList:result,
    });
    if(result == null || result.length<=0) {
      this.setData({
        showSearchLog: false,
      });
    } else {
      this.setData({
        showSearchLog: true,
      });
    }
  },

  //聚焦输入框时出发
  focusOnPut : function(e) {
    var data = this.data.searchLogList;
    console.log(data)
    if (data != null && data.length>0) {
      this.setData({
        showSearchLog: true,
        showLogList: data
      });
    }
  },


  //点击查询时出发
  balance_enquery : function(e) {
    var cardId = this.data.cardId;
    console.log(this.data.cardId);
    //卡号长度判断
    if(cardId.length!=10) {
      wx.showModal({
        title: '错误',
        content: "卡号长度为10",
        success: function (res) {
          if (res.confirm) {
          }
        }
      });
    } else {
      var dateTime = util.formatTime(new Date());
      //缓存
      console.log("缓存的cardNum:" + cardId)
      wx.setStorage({
        key: "cardNum",
        data: cardId
      });
      var logList = this.data.searchLogList;
      if (cardId != null && cardId != '' && !mathUtil.isArrayContains(logList, cardId)) {
        logList.push(cardId);
        this.setData({
          searchLogList: logList
        });
        wx.setStorage({
          key: "logCache",
          data: logList
        });
      }
      console.log(dateTime)
      wx.request({
        url: 'https://wxtest.gzyct.com:443/gw/base', //仅为示例，并非真实的接口地址
        method: 'POST',
        data: {
          busi_flag: "1",
          card_num: "510000" + cardId,
          channel_code: "70000023",
          charset: "UTF-8",
          rt_flag: "1",
          service: "yct.base.card.face.balance",
          sign: "D6B9AC55D193DC18148D0A535875DF00",
          sign_type: "MD5",
          timestamp: dateTime,
          user_id: "18611001106",
          version: 1.0
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        success: function (res) {
          var data = res.data;
          console.log(res.data)
          if (data.result_code == 0) {
            console.log(this)
            wx.navigateTo({
              url: '/pages/yct/balance-result/result?card_num=' + cardId + '&timestamp=' + data.timestamp + '&balance=' + data.balance + '&threshold=' + data.threshold,
            })
          } else {
            wx.showModal({
              title: '错误',
              content: data.err_msg,
              success: function (res) {
                if (res.confirm) {
                }
              }
            })
          }
        }
      })
    }
    
  },

  selectItem: function (e) {
    this.setData({
      cardId: e.currentTarget.dataset.log,
      showSearchLog:false
    })
  },

  blurOut: function(e) {
    this.setData({
      showSearchLog: false
    })
  },

  clearCache: function(e) {
    var d = this;
      wx.removeStorage({
        key: 'logCache',
        success: function (res) {
          console.log('清除历史记录' + { searchLogList: res.data } + { showLogList: res.data })
          d.setData({
            showLogList: [],
            searchLogList: [],
            showSearchLog: false,
            cardId: '',
          });
        }
      })
  }
}


Page(pageObject)